﻿using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS._4.ChaoBiaoGL
{
    public partial class ThirdPartyData : BasePage
    {
        #region 属性

        /// <summary>
        ///     保存结果集1
        /// </summary>
        private DataTable dtData
        {
            get { return ViewState["dtData"] as DataTable; }
            set { ViewState["dtData"] = value; }
        }

        #endregion

        #region 变量

        /// <summary>
        ///     数据访问操作类
        /// </summary>
        private readonly SpHelper _spHelper = new SpHelper();

        /// <summary>
        ///     用户信息BLL
        /// </summary>
        private readonly QS_YG_YONGHUXXBLL _YHXXBLL = new QS_YG_YONGHUXXBLL();

        /// <summary>
        ///     深井信息BLL
        /// </summary>
        private QS_YH_SHENJINGXXBBLL _SJXXBLL = new QS_YH_SHENJINGXXBBLL();

        /// <summary>
        ///     取水口信息bll
        /// </summary>
        private readonly QS_YH_QUSHUIKXXBBLL _QSKBLL = new QS_YH_QUSHUIKXXBBLL();

        /// <summary>
        ///     水表信息bll
        /// </summary>
        private readonly QS_YH_SHUIBIAOXXBBLL _SBXXBLL = new QS_YH_SHUIBIAOXXBBLL();

        /// <summary>
        ///     抄表数据BLL
        /// </summary>
        private QS_CB_CHAOBIAOSJ _CBSJ = new QS_CB_CHAOBIAOSJ();

        /// <summary>
        ///     抄表数据BLL
        /// </summary>
        private readonly QS_CB_CHAOBIAOSJBLL _CBSJBLL = new QS_CB_CHAOBIAOSJBLL();

        /// <summary>
        ///     日志BLL
        /// </summary>
        private readonly SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                txtSYM.Text= BusinessTool.GetSLYMorZWNY(1, 1).ToString();
                InitData();
                LoadData();
                Loadzs();
                LoadDataTxt(1);
                Display();
                txtChaoBiaoDate.Text = BusinessTool.GetDataBaseDateTime().ToString();
                // FocusChange();
            }
            if(dtData.Rows.Count<=0)
                gvList.DataSource = null;
            else
                gvList.DataSource = dtData;
            gvList.DataBind();
        }


        /// <summary>
        ///     修改操作
        /// </summary>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Status = GridViewStatus.Update;
            Display();
        }


        /// <summary>
        ///     保存验证
        /// </summary>
        /// <returns></returns>
        private string ValidateData()
        {
            var mg = "0";
            if (!PageTools.ValidateString(txtBenYueCH.Text, PageTools.eValidateType.数字))
            {
                mg = "1";
            }
            else if (!PageTools.ValidateString(txtBenYueSL.Text, PageTools.eValidateType.数字))
            {
                mg = "2";
            }

            else if (txtChaoBiaoZT.Text == "")
            {
                mg = "4";
            }
            else
            {
                if (txtYongHuH.Text != "6002023")
                {
                    //验证该水表是否为此用户的有效水表
                    var dt_sb = _SBXXBLL.SelectQS_YH_SHUIBIAOXXBsDynamic(string.Format(
                        @"i_jlzt=0 and s_shuibiaobh='{1}' and S_QUSHUIK='{2}' 
 and exists (select 1 from qs_yh_yonghusbgx where s_shuibiaoh='{1}' and s_yonghuh='{0}' and i_jlzt=0)",
                        txtYongHuH.Text, txtShuiBiaoBH.Text, txtJingBH.Text), null);
                    if (dt_sb.Rows.Count < 1)
                        mg = "3";
                    else
                        mg = "0";
                }
            }

            return mg;
        }

        #endregion

        #region 方法

        /// <summary>
        ///     初始化数据
        /// </summary>
        private void InitData()
        {
            //int wtype = BusinessTool.WType;
            span1.InnerText = "取水口编号";
            txtBenYueDL.ClientEnabled = true;
            txtBL.ClientEnabled = true;
            //txtKaiZhangDL.Visible = BusinessTool.WType == 1 ? true : false;
            //水量年月
            var ym = BusinessTool.GetSLYMorZWNY(1, 1);
            txtShuiLiangYM.Text = ym.ToString();
            LoadCH();
            txtYongHuH.Focus();


            var dt = _spHelper.P_QSCB_YZSFKZ(1, ym).Tables[0];
            if (dt != null && dt.Rows.Count > 0 && Convert.ToInt32(dt.Rows[0][0]) > 0)
            {
                btnSave.ClientEnabled = false;
                btnCancel.ClientEnabled = false;
            }
        }

        /// <summary>
        ///     加载册号
        /// </summary>
        private void LoadCH()
        {
            string[] arr1 = { "600" };
            for (var i = 0; i < arr1.Length; i++) cbmCeHao.Items.Add(arr1[i], arr1[i]);
        }

        /// <summary>
        ///     数据加载
        /// </summary>
        private void LoadData()
        {
           // var wtype = 1; // BusinessTool.WType;
            int ym = Convert.ToInt32(txtSYM.Text);
            dtData = _spHelper.QSCB_DSFANDSYSDATA_DBS(ym).Tables[0];
            gvList.DataSource = dtData;
            gvList.DataBind();
            if (dtData != null && dtData.Rows.Count > 0) zjl.Text = "共" + dtData.Rows.Count + "条";
        }

        /// <summary>
        ///     用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (Status)
            {
                case GridViewStatus.Init:
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    //this.treeChaoBiaoSH.Enabled = true;
                    //SetText(false);
                    break;
                case GridViewStatus.Search:
                    //SetText(false);
                    break;
                case GridViewStatus.Update:
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    //this.treeChaoBiaoSH.Enabled = false;
                    //SetText(true);
                    break;
                case GridViewStatus.Add:
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    //this.treeChaoBiaoSH.Enabled = false;
                    //SetText(true);
                    InitData();
                    break;
            }
        }


        /// <summary>
        ///     给输入框赋值
        /// </summary>
        private void SetData(DataTable dt)
        {
            var y = Convert.ToInt32(txtShuiLiangYM.Text) / 100;
            var m = Convert.ToInt32(txtShuiLiangYM.Text) % 100;
            var yhh = dt.Rows[0]["S_YONGHUH"].ToString();
            var sbh = dt.Rows[0]["S_SHUIBIAOBH"].ToString();
            var bh = dt.Rows[0]["BH"].ToString();
            var dz = dt.Rows[0]["DZ"].ToString();
            // DataTable dt_cb = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(string.Format("s_yonghuh='{0}' and s_shuibiaobh='{1}' and s_jingbh='{2}' and i_jlzt=0 and i_y={3} and i_m={4}", yhh, sbh, bh, y, m), null);
            var dt_cb = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(
                string.Format("s_yonghuh='{0}' and s_shuibiaobh='{1}'  and i_jlzt=0 and i_y={2} and i_m={3}", yhh, sbh,
                    y, m), null);

            var y_sy = 0;
            var m_sy = 0;
            if (m == 1)
            {
                m_sy = 12;
                y_sy = y - 1;
            }
            else
            {
                m_sy = m - 1;
                y_sy = y;
            }

            var dt_cb_sy = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(
                string.Format("s_yonghuh='{0}' and s_shuibiaobh='{1}' AND i_jlzt=0 and i_y={2} and i_m={3}", yhh, sbh,
                    y_sy, m_sy), null);

            txtJingBH.Text = bh;
            txtQuShuiDD.Text = dz;
            txtYongHuH.Text = yhh;
            txtYongHuM.Text = dt.Rows[0]["S_YONGHUM"].ToString();
            txtShuiJia.Text = dt.Rows[0]["N_SHUIJIA"].ToString();
            cbmCeHao.Text = dt.Rows[0]["S_CEHAO"].ToString();
            txtShuiBiaoBH.Text = sbh;
            var sllb = "";
            dt.Rows[0]["S_SHUILIANGLB"].ToString();
            //if (sllb == "1")
            //{
            //    txtShuiLiangLB.Text = "普通采";
            //}
            //else if (sllb == "2")
            //{
            //    txtShuiLiangLB.Text = "采灌采";
            //}
            //else if (sllb == "3")
            //{
            //    txtShuiLiangLB.Text = "采灌灌";
            //}
            //else if (sllb == "4")
            //{
            //    txtShuiLiangLB.Text = "矿泉采";
            //}
            //else if (sllb == "5")
            //{
            //    txtShuiLiangLB.Text = "专灌";
            //}
            txtChaoBiaoZT.Text = dt_cb.Rows.Count > 0 ? dt_cb.Rows[0]["I_CHAOBIAOZT"].ToString() : "0";
            txtBenYueCH.Text = dt_cb.Rows.Count > 0 ? dt_cb.Rows[0]["I_BENYUECM"].ToString() : "";
            txtBenYueSL.Text = dt_cb.Rows.Count > 0 ? dt_cb.Rows[0]["I_BENYUESL"].ToString() : "";
            txtShangYueCM.Text = dt_cb_sy.Rows.Count > 0 ? dt_cb_sy.Rows[0]["I_BENYUECM"].ToString() : "";
            txtShangYueSL.Text = dt_cb_sy.Rows.Count > 0 ? dt_cb_sy.Rows[0]["I_BENYUESL"].ToString() : "";
            txtKaiZhangSL.Text = dt_cb.Rows.Count > 0 ? dt_cb.Rows[0]["I_JIHUASL"].ToString() : "";
            txtBenYueDL.Text = dt_cb.Rows.Count > 0 ? dt_cb.Rows[0]["I_SHUILIANGDL"].ToString() : "";
            txtKaiZhangDL.Text = dt_cb.Rows.Count > 0 ? dt_cb.Rows[0]["I_JIHUASLDL"].ToString() : "";
            //cbmCeHao.Text = dt_cb.Rows.Count > 0 ? dt_cb.Rows[0]["S_CEHAO"].ToString() : "";
            txtBL.Text = GetBL(yhh);
            if (txtChaoBiaoZT.Text == "") txtChaoBiaoZT.Text = "0";
            txtBenYueCH.Focus();
        }


        /// <summary>
        ///     响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, CallbackEventArgs e)
        {
            var CH = cbmCeHao.Text;
            var wtype = BusinessTool.WType;
            var y = Convert.ToInt32(txtShuiLiangYM.Text) / 100;
            var m = Convert.ToInt32(txtShuiLiangYM.Text) % 100;
            var y_sy = 0;
            var m_sy = 0;
            if (m == 1)
            {
                m_sy = 12;
                y_sy = y - 1;
            }
            else
            {
                m_sy = m - 1;
                y_sy = y;
            }

            var msg = "";
            if (e.Parameter == "info")
            {
                var yhh = hdCollection.Get("yhh").ToString();
                var bl = GetBL(yhh);
                var dt_yh = _YHXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("i_jlzt=0 and s_yonghuh='{0}'", yhh),
                    null);
                if (dt_yh.Rows.Count < 1)
                {
                    msg = "0";
                }
                else
                {
                    var dt_qsk = _QSKBLL.SelectQS_YH_QUSHUIKXXBsDynamic(
                        string.Format(
                            "S_QUSHUIKBH in (select S_QUSHUIKH from QS_YH_YONGHUQSKGX where i_jlzt=0 and S_YONGHUH='{0}') and i_jlzt=0",
                            yhh), null);
                    var dt_sb = _SBXXBLL.SelectQS_YH_SHUIBIAOXXBsDynamic(
                        string.Format(
                            "S_SHUIBIAOBH in (select S_SHUIBIAOH from QS_YH_YONGHUSBGX where i_jlzt=0 and  S_YONGHUH='{0}') and i_jlzt=0",
                            yhh), null);

                    var bh = dt_qsk.Rows[0]["S_QUSHUIKBH"].ToString();
                    var dz = dt_qsk.Rows[0]["S_QUSHUIDZ"].ToString();
                    var dt_cb = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(
                        string.Format(
                            "s_yonghuh='{0}' and s_shuibiaobh='{1}' and s_jingbh='{2}' and i_jlzt=0 and i_y={3} and i_m={4}",
                            yhh, dt_sb.Rows[0]["S_SHUIBIAOBH"], bh, y, m), null);

                    var dt_cb_sy = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(
                        string.Format("s_yonghuh='{0}' and s_shuibiaobh='{1}'  and i_jlzt=0 and i_y={2} and i_m={3}",
                            yhh, dt_sb.Rows[0]["S_SHUIBIAOBH"], y_sy, m_sy), null);
                    var sllb = ""; // getSllb(dt_sb.Rows[0]["S_SHUILIANGLB"].ToString());

                    if (dt_cb.Rows.Count > 0 && dt_cb_sy.Rows.Count > 0)
                    {
                        var cbzt = dt_cb.Rows[0]["I_CHAOBIAOZT"].ToString() != ""
                            ? dt_cb.Rows[0]["I_CHAOBIAOZT"].ToString()
                            : "0";
                        msg = string.Join("|", dt_yh.Rows[0]["S_YONGHUM"].ToString(),
                            dt_yh.Rows[0]["N_SHUIJIA"].ToString(), bh, dz,
                            dt_sb.Rows[0]["S_CEHAO"].ToString(), dt_sb.Rows[0]["S_SHUIBIAOBH"].ToString(), sllb,
                            cbzt, dt_cb.Rows[0]["I_BENYUECM"].ToString(), dt_cb.Rows[0]["I_BENYUESL"].ToString(),
                            dt_cb_sy.Rows[0]["I_BENYUECM"].ToString(), dt_cb_sy.Rows[0]["I_BENYUESL"].ToString(),
                            dt_cb.Rows[0]["I_JIHUASL"].ToString(), dt_cb.Rows[0]["I_SHUILIANGDL"].ToString(),
                            dt_cb.Rows[0]["I_JIHUASLDL"].ToString(), yhh, bl
                        );
                    }
                    else
                    {
                        var cm_sy = dt_cb_sy.Rows.Count > 0 ? dt_cb_sy.Rows[0]["I_BENYUECM"].ToString() : "";
                        var sl_sy = dt_cb_sy.Rows.Count > 0 ? dt_cb_sy.Rows[0]["I_BENYUESL"].ToString() : "";

                        msg = string.Join("|", dt_yh.Rows[0]["S_YONGHUM"].ToString(),
                            dt_yh.Rows[0]["N_SHUIJIA"].ToString(), bh, dz,
                            dt_sb.Rows[0]["S_CEHAO"].ToString(), dt_sb.Rows[0]["S_SHUIBIAOBH"].ToString(), sllb,
                            "", "", "", cm_sy, sl_sy, "", "", "", yhh, bl);
                    }
                }
            }
            //  this.treeChaoBiaoSH.FindNodeByKeyValue(yhh).Focus();
            else if (e.Parameter == "setinfo")
            {
                var id = Convert.ToInt32(hdCollection.Get("ID"));
                var dt = _spHelper.P_QSCB_CHAOBIAOSJ(wtype, id).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    var yhh2 = dt.Rows[0]["S_YONGHUH"].ToString();
                    var bl = GetBL(yhh2);
                    var sbh = dt.Rows[0]["S_SHUIBIAOBH"].ToString();
                    //井编号或者取水口编号

                    var QSKorJBH =
                        _SBXXBLL.SelectQS_YH_SHUIBIAOXXBsDynamic(
                                string.Format("I_JLZT=0 AND I_WTYPE={0} AND S_SHUIBIAOBH='{1}'", wtype, sbh), null)
                            .Rows[0]["S_QUSHUIK"].ToString();

                    var dt_qsk = wtype == 1
                        ? _QSKBLL.SelectQS_YH_QUSHUIKXXBsDynamic(
                            string.Format("S_QUSHUIKBH='{0}' and i_jlzt=0", QSKorJBH), null)
                        : null;
                    var bh = dt_qsk.Rows.Count > 0 ? dt_qsk.Rows[0]["S_QUSHUIKBH"].ToString() : "";
                    var dz = dt_qsk.Rows.Count > 0 ? dt_qsk.Rows[0]["S_QUSHUIDZ"].ToString() : "";

                    var dt_cb_sy = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(
                        string.Format("s_yonghuh='{0}' and s_shuibiaobh='{1}'  and i_jlzt=0 and i_y={2} and i_m={3}",
                            yhh2, sbh, y_sy, m_sy), null);
                    var sllb = "";
                    var cbzt = dt.Rows[0]["I_CHAOBIAOZT"].ToString() != ""
                        ? dt.Rows[0]["I_CHAOBIAOZT"].ToString()
                        : "0";
                    var cm_sy = dt_cb_sy.Rows.Count > 0 ? dt_cb_sy.Rows[0]["I_BENYUECM"].ToString() : "0";
                    var sl_sy = dt_cb_sy.Rows.Count > 0 ? dt_cb_sy.Rows[0]["I_BENYUESL"].ToString() : "0";
                    msg = string.Join("|", dt.Rows[0]["S_YONGHUM"].ToString(), dt.Rows[0]["N_SHUIJIA"].ToString(), bh,
                        dz,
                        dt.Rows[0]["S_CEHAO"].ToString(), sbh, sllb,
                        cbzt, dt.Rows[0]["I_BENYUECM"].ToString(), dt.Rows[0]["I_BENYUESL"].ToString(),
                        cm_sy, sl_sy,
                        dt.Rows[0]["I_JIHUASL"].ToString(), dt.Rows[0]["I_SHUILIANGDL"].ToString(),
                        dt.Rows[0]["I_JIHUASLDL"].ToString(), yhh2, bl
                    );
                }
            }
            else if (e.Parameter == "first")
            {
                var ch = hdCollection.Get("ch").ToString();
                var dt = _spHelper.P_QSCB_CHAOBIAOSJ2(wtype, 1, CH).Tables[0];
                if (dt.Rows.Count > 0) msg = SetText(dt, y, m, y_sy, m_sy);
            }
            else if (e.Parameter == "last" || e.Parameter == "next" || e.Parameter == "final")
            {
                var rn = Convert.ToInt32(hdCollection.Get("RN"));
                var dt = _spHelper.P_QSCB_CHAOBIAOSJ2(wtype, rn, CH).Tables[0];
                if (dt.Rows.Count > 0) msg = SetText(dt, y, m, y_sy, m_sy);
            }
            else if (e.Parameter == "save")
            {
                msg = ValidateData();
                if (msg == "0") //保存数据
                {
                    OperateState operateState;
                    var id = 0;
                    var dt_cb = new DataTable();
                    //if (txtYongHuH.Text == "6002023")
                    //{
                    //    dt_cb = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(string.Format("s_yonghuh='{0}' and s_shuibiaobh='{1}'  and i_jlzt=0 and  i_y={2} and i_m={3}", txtYongHuH.Text, txtShuiBiaoBH.Text, y, m), null);
                    //}
                    //else
                    //{
                    dt_cb = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(
                        string.Format("s_yonghuh='{0}' and s_shuibiaobh='{1}' and i_jlzt=0 and  i_y={2} and i_m={3}",
                            txtYongHuH.Text, txtShuiBiaoBH.Text, y, m), null);
                    //}
                    if (dt_cb.Rows.Count > 0)
                    {
                        id = Convert.ToInt32(dt_cb.Rows[0]["ID"]);
                        //修改操作
                        operateState = OperateState.Modify;
                        _CBSJ = _CBSJBLL.SelectQS_CB_CHAOBIAOSJ(id);
                        _CBSJ.ID = id;
                    }
                    //添加
                    else
                    {
                        operateState = OperateState.Add;
                        _CBSJ.ID = 0;
                        _CBSJ.S_SHUIBIAOBH = txtShuiBiaoBH.Text;
                        _CBSJ.I_Y = y;
                        _CBSJ.I_M = m;
                        _CBSJ.I_JIHUASL = Convert.ToDecimal(txtBenYueSL.Text);
                        _CBSJ.S_YONGHUH = txtYongHuH.Text;
                        _CBSJ.S_JINGBH = txtJingBH.Text != "" ? txtJingBH.Text : "";
                        var sjbll = new QS_YH_QUSHUIKXXBBLL();
                        var dt_Q = sjbll.SelectQS_YH_QUSHUIKXXBsDynamic(
                            string.Format(" i_jlzt=0 and S_QUSHUIKBH='{0}'", txtJingBH.Text), null);
                        var dt_yh = _YHXXBLL.SelectQS_YG_YONGHUXXesDynamic(
                            string.Format("i_jlzt=0 and i_wtype=1 and s_yonghuh='{0}'", txtYongHuH.Text), null);

                        var bmbh = dt_yh.Rows.Count > 0 ? dt_yh.Rows[0]["S_BUMENBH"].ToString() : "";
                        var qsyt = dt_yh.Rows.Count > 0 ? dt_yh.Rows[0]["S_QUSHUIYTBH"].ToString() : "";
                        var hybh = dt_yh.Rows.Count > 0 ? dt_yh.Rows[0]["S_HANGYEBH"].ToString() : "";
                        var xzq = dt_Q.Rows.Count > 0 ? dt_Q.Rows[0]["S_XINGZHENGQBH"].ToString() : "";
                        var lybh = dt_Q.Rows.Count > 0 ? dt_Q.Rows[0]["S_LIUYUBH"].ToString() : "";
                        var hxbj = dt_Q.Rows.Count > 0 ? dt_yh.Rows[0]["S_HUANXIANBJ"].ToString() : "";
                        var xkz = dt_Q.Rows.Count > 0 ? dt_Q.Rows[0]["S_XUKEZBH"].ToString() : "";
                        _CBSJ.S_BUMENBH = bmbh;
                        _CBSJ.S_QUSHUIYTBH = qsyt;
                        _CBSJ.S_HANGYEBH = hybh;
                        _CBSJ.S_XINGZHENGQBH = xzq;
                        _CBSJ.S_LIUYUBH = lybh;
                        _CBSJ.S_HUANXIANBJ = hxbj;
                        _CBSJ.S_XUKEZBH = xkz;
                        _CBSJ.S_JINGLEIXBH = "";
                        _CBSJ.S_JINGBIAOSBH = "";
                        _CBSJ.S_JINGZHUANGTBH = "";
                        _CBSJ.S_CEHAO = cbmCeHao.Text;
                        _CBSJ.S_SHUILIANGLB = txtShuiLiangLB.Text;
                        _CBSJ.S_TIANJIAR = LoginInfo.CurrentUser.UserID.ToString();
                        _CBSJ.D_TIANJIARQ = Convert.ToDateTime(txtChaoBiaoDate.Text);//BusinessTool.GetDataBaseDateTime();
                        _CBSJ.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                        _CBSJ.D_CAOZUORQ = BusinessTool.GetDataBaseDateTime();
                        _CBSJ.N_SHUIJIA = Convert.ToDecimal(txtShuiJia.Text);
                    }

                    _CBSJ.I_JLZT = 0;
                    _CBSJ.I_CHAOBIAOZT = Convert.ToDecimal(txtChaoBiaoZT.Text);
                    _CBSJ.I_BENYUECM = Convert.ToDecimal(txtBenYueCH.Text);
                    _CBSJ.I_BENYUESL = Convert.ToDecimal(txtBenYueSL.Text);
                    _CBSJ.I_JIHUASL =
                        Convert.ToDecimal(txtKaiZhangSL
                            .Text); //Convert.ToDecimal(txtBenYueSL.Text) * (Convert.ToDecimal(txtBL.Text) / 1000);//开帐水量
                    _CBSJ.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                    _CBSJ.D_CAOZUORQ = Convert.ToDateTime(txtChaoBiaoDate.Text);// BusinessTool.GetDataBaseDateTime();
                    _CBSJ.I_SHUILIANGDL = txtBenYueDL.Text == "" ? 0 : Convert.ToDecimal(txtBenYueDL.Text);

                    //同时修改水表信息表里面的册号
                    // string res = "";
                    //_spHelper.P_QSCB_XGCH(wtype, txtShuiLiangLB.Text, cbmCeHao.Text, out res);


                    _SYS_LOGBLL.Log(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "抄表数据录入", operateState, _CBSJ,
                        null);
                    var result = _CBSJBLL.InsertOrUpdate(_CBSJ);
                    //执行操作
                    if (result)
                    {
                        Status = GridViewStatus.Init;
                        msg = "t";
                    }
                }
            }

            e.Result = e.Parameter + "|0|" + msg;
        }

        /// <summary>
        ///     返回字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="y"></param>
        /// <param name="m"></param>
        /// <param name="y_sy"></param>
        /// <param name="m_sy"></param>
        /// <returns></returns>
        private string SetText(DataTable dt, int y, int m, int y_sy, int m_sy)
        {
            var msg = "";
            var yhh = dt.Rows[0]["S_YONGHUH"].ToString();
            var sbh = dt.Rows[0]["S_SHUIBIAOBH"].ToString();
            var bh = dt.Rows[0]["BH"].ToString();
            var dz = dt.Rows[0]["DZ"].ToString();
            var bl = GetBL(yhh);

            var dt_cb = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(
                string.Format("s_yonghuh='{0}' and s_shuibiaobh='{1}' and  i_jlzt=0 and i_y={2} and i_m={3}", yhh, sbh,
                    y, m), null);
            var dt_cb_sy = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(
                string.Format("s_yonghuh='{0}' and s_shuibiaobh='{1}' and  i_jlzt=0 and i_y={2} and i_m={3}", yhh, sbh,
                    y_sy, m_sy), null);
            var sllb = dt.Rows[0]["S_SHUILIANGLB"].ToString();
            var sllb2 = "";
            if (dt_cb.Rows.Count > 0 && dt_cb_sy.Rows.Count > 0)
            {
                var cbzt = dt_cb.Rows[0]["I_CHAOBIAOZT"].ToString() != ""
                    ? dt_cb.Rows[0]["I_CHAOBIAOZT"].ToString()
                    : "0";
                msg = string.Join("|", dt.Rows[0]["S_YONGHUM"].ToString(), dt.Rows[0]["N_SHUIJIA"].ToString(), bh, dz,
                    dt.Rows[0]["S_CEHAO"].ToString(), sbh, sllb2,
                    cbzt, dt_cb.Rows[0]["I_BENYUECM"].ToString(), dt_cb.Rows[0]["I_BENYUESL"].ToString(),
                    dt_cb_sy.Rows[0]["I_BENYUECM"].ToString(), dt_cb_sy.Rows[0]["I_BENYUESL"].ToString(),
                    dt_cb.Rows[0]["I_JIHUASL"].ToString(), dt_cb.Rows[0]["I_SHUILIANGDL"].ToString(),
                    dt_cb.Rows[0]["I_JIHUASLDL"].ToString(), yhh, bl);
            }
            else if (dt_cb.Rows.Count > 0 && dt_cb_sy.Rows.Count < 1)
            {
                var cbzt = dt_cb.Rows[0]["I_CHAOBIAOZT"].ToString() != ""
                    ? dt_cb.Rows[0]["I_CHAOBIAOZT"].ToString()
                    : "0";
                msg = string.Join("|", dt.Rows[0]["S_YONGHUM"].ToString(), dt.Rows[0]["N_SHUIJIA"].ToString(), bh, dz,
                    dt.Rows[0]["S_CEHAO"].ToString(), sbh, sllb2,
                    cbzt, dt_cb.Rows[0]["I_BENYUECM"].ToString(), dt_cb.Rows[0]["I_BENYUESL"].ToString(),
                    "0", "0",
                    dt_cb.Rows[0]["I_JIHUASL"].ToString(), dt_cb.Rows[0]["I_SHUILIANGDL"].ToString(),
                    dt_cb.Rows[0]["I_JIHUASLDL"].ToString(), yhh, bl);
            }
            else
            {
                var bycm_sy = dt_cb_sy.Rows.Count > 0 ? dt_cb_sy.Rows[0]["I_BENYUECM"].ToString() : "0";
                var bysl_sy = dt_cb_sy.Rows.Count > 0 ? dt_cb_sy.Rows[0]["I_BENYUESL"].ToString() : "0";

                msg = string.Join("|", dt.Rows[0]["S_YONGHUM"].ToString(), dt.Rows[0]["N_SHUIJIA"].ToString(), bh, dz,
                    dt.Rows[0]["S_CEHAO"].ToString(), sbh, sllb2,
                    "0", "", "",
                    bycm_sy, bysl_sy,
                    "", "", "", yhh, bl);
            }

            return msg;
        }


        /// <summary>
        ///     总数
        /// </summary>
        private void Loadzs()
        {
            var wtype = BusinessTool.WType;
            var dt = _spHelper.P_QSCB_CHAOBIAOSJ2(wtype, 0, "0").Tables[0];
            var rs = dt.Rows.Count > 0 ? Convert.ToInt32(dt.Rows[0][0]) : 1;
            rows.Value = rs.ToString();
            lbzjl.Text = "共" + rs + "条";
        }

        private void LoadDataTxt(int rn)
        {
            var CH = cbmCeHao.Text == "" ? "0" : cbmCeHao.Text;
            row.Value = rn.ToString();
            var wtype = BusinessTool.WType;
            var dtSouce = _spHelper.P_QSCB_CHAOBIAOSJ2(wtype, rn, CH).Tables[0];
            if (dtSouce.Rows.Count > 0) SetData(dtSouce);
        }

        protected void gvList_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData();
        }

        private string GetBL(string yhh)
        {
            // 086F34BF-6DF0-4653-955C-5DBE4A7351E0	01	100%水量
            //1814067B-BEDE-4595-920D-E512329C3576	02	1%水量
            //36C72A6F-3DBA-4A82-A33A-3DBB24F57588	03	从电量
            //5ECCF720-63E9-42E0-B28C-4666C506784E		

            var dt = _YHXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("i_jlzt=0 and s_yonghuh='{0}'", yhh), null);
            var type = dt.Rows.Count > 0 ? dt.Rows[0]["S_FEETYPE"].ToString() : "";
            var bl = "";
            if (type == "02" || type == "1%水量")
                bl = "10";
            else if (type == "01" || type == "100%水量") //086F34BF-6DF0-4653-955C-5DBE4A7351E0
                bl = "1000";
            else if (type == "03" || type == "从电量") //36C72A6F-3DBA-4A82-A33A-3DBB24F57588
                bl = "";
            else
                bl = "";

            return bl;
        }

        protected void UpLoadFile_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
        {
            e.CallbackData = "操作失败";
            var userid = LoginInfo.CurrentUser.UserID;
            //e.IsValid = false;
            const string UploadDirectory = "/UPLOAD/DXS/CB/"; //图片上传文件夹路径
            if (!UpLoadFile.IsValid)
            {
                //return string.Empty;
            }
            else
            {
                var fileName = Guid.NewGuid() + Path.GetExtension(UpLoadFile.FileName); //文件名
                var path = Server.MapPath(UploadDirectory);
                if (Directory.Exists(path) == false)
                {
                    Directory.CreateDirectory(path);
                }
                var filePath = Path.Combine(path, fileName); //文件完整物理路径
                UpLoadFile.SaveAs(filePath);
                var dt = ReadExcel(filePath);

                if (dt != null && dt.Rows.Count > 0)
                {
                    var newdt = _spHelper.P_QSCB_DRSJ2(userid, 0).Tables[0];
                    foreach (DataRow dr in dt.Rows)
                    {
                        var newdr = newdt.NewRow();
                        if (dt.Columns.Contains("水量年")) newdr["I_Y"] = dr["水量年"];
                        if (dt.Columns.Contains("水量月")) newdr["I_M"] = dr["水量月"];
                        if (dt.Columns.Contains("用户号")) newdr["S_YONGHUH"] = dr["用户号"];
                        if (dt.Columns.Contains("用户名")) newdr["S_YONGHUM"] = dr["用户名"];
                        if (dt.Columns.Contains("井编号")) newdr["S_JINGBH"] = dr["井编号"];
                        if (dt.Columns.Contains("册号")) newdr["S_CEHAO"] = dr["册号"];
                        if (dt.Columns.Contains("水表编号")) newdr["S_SHUIBIAOBH"] = dr["水表编号"];
                        if (dt.Columns.Contains("水量类别")) newdr["S_SHUILIANGLB"] = dr["水量类别"];
                        if (dt.Columns.Contains("抄表状态")) newdr["I_CHAOBIAOZT"] = dr["抄表状态"];
                        if (dt.Columns.Contains("本月抄码")) newdr["I_BENYUECM"] = dr["本月抄码"];
                        if (dt.Columns.Contains("本月水量")) newdr["I_BENYUESL"] = dr["本月水量"];
                        if (dt.Columns.Contains("本月抄表日")) newdr["D_CAOZUORQ"] = dr["本月抄表日"];
                        newdt.Rows.Add(newdr);
                    }

                    var tablename = "PLANWATER_TEMP.QSCB_DRSJ";
                    if (BusinessTool.InsertOracelData(newdt, tablename))
                    {
                        //e.CallbackData = "1111";

                        var ds = _spHelper.QSCB_DiSanFangDRSJ(userid, 1);
                        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                        {
                            e.IsValid = true;
                            e.CallbackData = ds.Tables[0].Rows[0][0].ToString();
                        }
                    }
                }
            }
        }

        private DataTable ReadExcel(string ExcelFilePath)
        {
            var dt = new DataTable();
            //string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +"Data Source=" +ExcelFilePath + ";Extended Properties=Excel 8.0";
            var strConnection =
                "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';data source=" +
                ExcelFilePath;
            var Connection = new OleDbConnection(strConnection);
            Connection.Open();
            var CommandText = "";
            var dtsheetName = GetWorkBookName(Connection);
            if (dtsheetName != null && dtsheetName.Rows.Count > 0)
                CommandText = "select * from [" + dtsheetName.Rows[0]["TABLE_NAME"] + "]";
            else
                return null;
            var comm = new OleDbCommand(CommandText, Connection);

            try
            {
                var da = new OleDbDataAdapter(comm);
                var Ds = new DataSet();
                da.Fill(Ds);
                dt = Ds.Tables[0];
            }
            catch (Exception)
            {
            }
            finally
            {
                if (Connection.State == ConnectionState.Open) Connection.Close();
            }

            return dt;
        }

        /// <summary>
        ///     获取Excel中的所有工作簿
        /// </summary>
        /// <param name="Connection">OleDbConnection对象</param>
        /// <returns></returns>
        public static DataTable GetWorkBookName(OleDbConnection Connection)
        {
            DataTable Dt = null;
            try
            {
                if (Connection.State == ConnectionState.Closed) Connection.Open();
                Dt = Connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            }
            catch (Exception)
            {
            }
            finally
            {
                if (Connection.State == ConnectionState.Open) Connection.Close();
            }

            return Dt;
        }
        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
            if (dtData.Rows.Count <= 0)
                gvList.DataSource = null;
            else
                gvList.DataSource = dtData;
            gvList.DataBind();
            zjl.Text = "共" + dtData.Rows.Count + "条";
        }

        protected void btnOutExcel_Click(object sender, EventArgs e)
        {
            if (this.dtData == null || this.dtData.Rows.Count < 1)
            {
                PageTools.AlertMsg(this, "没有符合的数据导出");
            }
            else
            {
                ASPxGridViewExporter1.GridViewID = "gvList";
                string sName =  "第三方水量年月" + txtSYM.Text + "抄表数据";
                gvList.Settings.ShowFooter = false;
                ASPxGridViewExporter1.WriteXlsToResponse(sName);
            }
        }
    }
}